#include<string.h>

#include"Page.h"

int PageScreenOutput(PageTypedef* _page)
{
    int i = 0;
    if(PutBgLayer(_page->bgPicture) < 0)
    {
        printf("Put BG layer fail\n");
        return -1;
    }
    // printf("Put BG succceed\n");

    /* 输出按键图标 */
    for(i=0;i<MaxPageButtons;i++)
    {
        if(_page->Buttons[i] != NULL)
        {
            if(ButtonPictureUpdate(_page->Buttons[i]) < 0)
            {
                printf("Put Button layer %d fail\n", i);
                return -1;
            }
            else{}
            // printf("Put BT%d success \n", i);
        }
        else{}
    }

    /* 输出字符串图片 */
    for(i=0;i<MaxPageStrings;i++)
    {
        if(_page->Strings[i] != NULL)
        {
            if(switchString2Rgba(_page->Strings[i]) < 0)
            {
                printf("Put string layer %d fail\n", i);
                return -1;
            }
            else{}
            if(AddLayer(&_page->Strings[i]->srcLayer, _page->Strings[i]->dstPos.centerLeft,  _page->Strings[i]->dstPos.centerTop) == 0)
            { 
                // printf("Put STR%d success \n", i);
            }
        }
        else{}
    }

    ScreenOutput();
    for(i=0;i<MaxPageStrings;i++)
    {
        if(_page->Strings[i] != NULL)
        {
            freeStringMem(_page->Strings[i]);
        }
        else{}
    }

    // printf("output success\n");
    return 0;
}

int PageCheckButtons(PageTypedef* _page)
{
    int ret;
    int i;
    int statusChange = 0;
    struct ak_ts_event tmpEvent;
    touchPositionTyp tmpPosition;
    ret = touchScreenGetEvent(&tmpEvent, 30);
    if(ret == 0)
    {
        for(i=0;i<MaxPageButtons;i++)
        {
            if(_page->Buttons[i] != NULL)
            {
                /* 若检测到有变化，statusChange赋1 */
                if(ButtonTouchCheck(_page->Buttons[i], &tmpEvent))
                {
                    statusChange = 1;
                } 
                else{}
            }
            else{}
        }
    }
    
    return statusChange;
}

int PageResetButtons(PageTypedef* _page)
{
    int i;
    for(i=0;i<MaxPageButtons;i++)
    {
        if(_page->Buttons[i] != NULL)
        {
            if(_page->Buttons[i]->status == ButtonToOn)
                _page->Buttons[i]->status = ButtonOff;
            else if(_page->Buttons[i]->status == ButtonToOff)
                _page->Buttons[i]->status = ButtonOn;
            else if(_page->Buttons[i]->status == ButtonTouching)
                _page->Buttons[i]->status = ButtonOff;

            if(_page->Buttons[i]->jumpType == Jump_Button)
                _page->Buttons[i]->status = ButtonOff;
        }
    }
}